#-*- coding = utf-8 -*-
#@Time: 2020/4/10 18:37
#@Author: mx
#@File: getcityname.py
#@Software: IntelliJ IDEA
from lxml import etree

from bs4 import BeautifulSoup
import requests
from lib.request.headers import create_headers
from lib.utility.path import get_root_path
from lib.item.cityinfo import *
import csv

def get_city_cnfo():
    name_list = list()

    url = 'https://www.ke.com/city'
    response = requests.get(url, headers=create_headers())
    html = response.content
    soup = BeautifulSoup(html, 'lxml')
    # links = soup.find_all("li",class_="CLICKDATA")
    links = soup.find_all("ul",class_="city_list_ul")
    for x in range(0,1):
        tmp = links[0]
        result = tmp.find_all("a")
        for res in result:
            # print(res.attrs['href'])
            name_dict = dict()
            if res.attrs['href'].find('.fang.') >= 0:
                state = '1'
            else:
                state = '0'
            li = res.attrs['href'].split(".")
            name_dict['city_name'] = res.contents[0]
            name_dict['city_py'] = li[0][2:]
            name_dict['city_state'] = state

            city_name = name_dict['city_name']
            city_py = name_dict['city_py']
            city_state = name_dict['city_state']
            cityInfo = CITYINFO(city_name,city_py,city_state)
            name_list.append(cityInfo)
    #数据写入csv
    city_file = get_root_path()+"/data/city_name.csv"
    with open(city_file,'w') as f:
        for cityinfo in  name_list:
            f.write(cityinfo.text() + "\n")
if __name__ == '__main__':
    get_city_cnfo()